﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QuaManSys.InnerInfo
{
    public partial class InnerInfoReminderBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] != null)
            {
                //根据当前用户的身份显示消息提醒栏
                Tab_Person user = (Tab_Person)Session["CurUser"];
                string dept = user.department;
                ShowReminderList(dept);
            }
            else
            {
                //跳转到登录页面
                Response.Redirect("~/Login.aspx");
            } 
        }

        private void ShowReminderList(string department)
        {
            if ("QA" == department)
            {
                using (QuaManSysEntities context = new QuaManSysEntities())
                {
                    try
                    {
                        pnlReminder1.GroupingText = "待分发列表";

                        var inInfoDis =
                            from c in context.InnerQualityInfo
                            where c.processingStatus == "分发"
                            select new { qualityInfoId = c.qualityInfoId };

                        int i = 1;
                        string disUrl = "InnerInfoDistribute.aspx";
                        foreach (var item in inInfoDis)
                        {
                            FillPanelWithControl(pnlReminder1, i++, item.qualityInfoId, "分发", disUrl);
                        }

                        pnlReminder2.GroupingText = "待闭环列表";

                        var inInfoFile =
                            from c in context.InnerQualityInfo
                            where c.processingStatus == "处理"
                            select new { qualityInfoId = c.qualityInfoId };

                        int j = 1;
                        string fileUrl = "InnerInfoProcessing.aspx";
                        foreach (var item in inInfoFile)
                        {
                            FillPanelWithControl(pnlReminder2, j++, item.qualityInfoId, "闭环", fileUrl);
                        }
                    }
                    catch
                    {
                        //todo:
                    }
                }
            }
            else
            {
                using (QuaManSysEntities context = new QuaManSysEntities())
                {
                    try
                    {
                        pnlReminder1.GroupingText = "待处理列表--责任部门";

                        var inInfoResPro =
                            from c in context.InnerQualityInfo
                            join o in context.InnerResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                            where c.processingStatus == "处理" && department == o.department
                                && o.isResponDepart == 1
                            select c;

                        int i = 1;
                        string proUrl = "InnerInfoProcessing.aspx";
                        foreach (var item in inInfoResPro)
                        {
                            FillPanelWithControl(pnlReminder1, i++, item.qualityInfoId, "处理", proUrl);
                        }

                        pnlReminder2.GroupingText = "待处理列表--关注部门";

                        var inInfoAttPro =
                            from c in context.InnerQualityInfo
                            join o in context.InnerResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                            where c.processingStatus == "处理" && department == o.department
                                && o.isResponDepart == 0
                            select c;
                        int j = 1;
                        foreach (var item in inInfoAttPro)
                        {
                            FillPanelWithControl(pnlReminder2, j++, item.qualityInfoId, "处理", proUrl);
                        }
                    }
                    catch
                    {
                        //todo:
                    }
                }
            }
        }
        /// <summary>
        /// 根据数据库里的记录，自动在panel生成仅显示信息编号的列表
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="i"></param>
        /// <param name="infoId"></param>
        private void FillPanelWithControl(Panel panel, int i, string infoId, string needTo, string url)
        {
            Label myLabel = new Label();
            panel.Controls.Add(myLabel);
            myLabel.Text = (i++).ToString() + " . 质量信息编号为：" +
                infoId + "需要" + needTo + "。";

            HyperLink innerHref = new HyperLink();
            panel.Controls.Add(innerHref);
            innerHref.Text = "立即处理。";
            innerHref.NavigateUrl = url + "?id=" +
                infoId;
            panel.Controls.Add(new LiteralControl("<br />"));
        }
    }
}